﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace XLB.ExtensionMethodCore
{
    public class EmailHelper
    {
        /// <summary>
        /// 邮箱smtp服务密码
        /// <remarks>确保邮箱已经开启了SMTP服务，开启后会给出一串编码就是smtp服务密码，后台填入编码</remarks>
        /// </summary>
        public static readonly string SMIPServiceCode = "ddfczrbawgpbcaeg";
        
        /// <summary>
        /// 发送信息的邮箱
        /// </summary>
        public static readonly string SendMailbox = "428616818@qq.com";

        /// <summary>
        /// 邮箱服务器类型
        /// </summary>
        public static readonly string Host = "smtp.qq.com";

        /// <summary>
        /// 邮箱服务器端口
        /// </summary>
        public static readonly int Port = 587;

        /// <summary>
        /// 向指定邮箱发送验证码
        /// </summary>        
        /// <param name="receivingMailbox">接收邮箱</param>
        public static void SendEmailValidateCode(string receivingMailbox)
        {      
            string iRdm = XLBCommon.Random(6);
            SendInfoToSpecifiedMailbox(receivingMailbox, "登录验证码", "您现在正在注册***小程序，您的验证码为："+iRdm+"。验证码等同于密码，请勿泄露与他人。");
        }

        /// <summary>
        /// 向指定邮箱发送信息
        /// </summary>
        /// <param name="receivingMailbox">接收者邮箱</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="mailContent">邮件内容</param>
        public static void SendInfoToSpecifiedMailbox(string receivingMailbox,string subject,string mailContent)
        {
            //SendMailbox:发送信息的邮箱
            //SMIPServiceCode:邮箱smtp服务密码，确保邮箱已经开启了SMTP服务，开启后会给出一串编码就是smtp服务密码，后台填入编码
            //Host:邮箱服务器类型,我这里用的是QQ邮箱：smtp.qq.com
            //Port:邮箱服务器端口
            SmtpClient client = new SmtpClient(Host, Port);           
            MailMessage msg = new MailMessage(SendMailbox, receivingMailbox, subject, mailContent);
            client.UseDefaultCredentials = false;
            System.Net.NetworkCredential basicAuthenticationInfo =
            new System.Net.NetworkCredential(SendMailbox, SMIPServiceCode);
            client.Credentials = basicAuthenticationInfo;
            client.EnableSsl = true;
            client.Send(msg);
        }
    }
}
